﻿using Domain.Entities;
using Domain.Repository;
using Domain.Repository.Repositories;
using NHibernate;
using NHibernate.Expression;
using System;
using System.Collections.Generic;

namespace Domain.Repository.Implementations.NHibernate
{
    public class GroupRepository : RepositoryBase, IGroupRepository
    {
        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public IList<Group> GetAll()
        {
            return Session.CreateCriteria(typeof(Group)).AddOrder(Order.Asc("Name")).List<Group>();
        }

        public Group GetById(short groupId)
        {
            throw new NotImplementedException();
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        public Group New(string name, string prefix, string suffix, bool canViewActiveUserlist, bool canViewUserlist, bool canViewProfiles, bool canUseSearch, bool isAdministratorGroup)
        {
            using (ITransaction transaction = Session.BeginTransaction())
            {
                Group group = new Group();
                group.Name = name;
                group.Prefix = prefix;
                group.Suffix = suffix;
                group.CanViewActiveUserlist = canViewActiveUserlist;
                group.CanViewUserlist = canViewUserlist;
                group.CanViewProfiles = canViewProfiles;
                group.CanUseSearch = canUseSearch;
                group.IsAdministratorGroup = isAdministratorGroup;
                Session.SaveOrUpdate(group);

                transaction.Commit();

                return group;
            }
        }

        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public void Update(Group group, string name, string prefix, string suffix, bool canViewActiveUserlist, bool canViewUserlist, bool canViewProfiles, bool canUseSearch, bool isAdministratorGroup)
        {
            using (ITransaction transaction = Session.BeginTransaction())
            {
                group.Name = name;
                group.Prefix = prefix;
                group.Suffix = suffix;
                group.CanViewActiveUserlist = canViewActiveUserlist;
                group.CanViewUserlist = canViewUserlist;
                group.CanViewProfiles = canViewProfiles;
                group.CanUseSearch = canUseSearch;
                group.IsAdministratorGroup = isAdministratorGroup;
                Session.SaveOrUpdate(group);

                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/ 
    }
}
